package com.hui.auth.service.impl;

import com.hui.auth.dao.AuthPermissionDao;
import com.hui.auth.entity.AuthPermission;
import com.hui.auth.service.AuthPermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class AuthPermissionServiceImpl implements AuthPermissionService {

    @Autowired
    private AuthPermissionDao authPermissionDao;

    @Override
    public boolean insert(AuthPermission perm) {
        return authPermissionDao.insert(perm);
    }

    @Override
    public List<AuthPermission> selectList(Map params) {
        return authPermissionDao.selectList(params);
    }

    @Override
    public List<AuthPermission> selectActive() {
        return authPermissionDao.selectActive();
    }

    @Override
    public Integer update(AuthPermission perm) {
        return authPermissionDao.update(perm);
    }

    @Override
    public boolean delete(Long id) {
        if (1 == authPermissionDao.delete(id)) {
            return true;
        }
        return false;
    }

    @Override
    public List<AuthPermission> selectByRid(Long id) {
        return authPermissionDao.selectByRid(id);
    }

    @Override
    public List<AuthPermission> selectByMid(Long mid) {
        return authPermissionDao.selectByMid(mid);
    }

    @Override
    public List<Integer> selectPerms(Integer mid) {
        return authPermissionDao.selectPerms(mid);
    }

    @Override
    public boolean updateByIds(Integer mid, List<Integer> perms, List<AuthPermission> mapList) {
        authPermissionDao.updateByIds(mid, perms);
        for (AuthPermission p : mapList) {
            authPermissionDao.update(p);
        }
        return true;
    }

    @Override
    public List<AuthPermission> selectNoUse(Map params) {
        return authPermissionDao.selectNoUse(params);
    }

    @Override
    public boolean updateByRids(List<Integer> permsData, Integer rid, List<Integer> pids) {
        for (int pid : permsData) {
            authPermissionDao.deleteByRidAndPid(rid, pid);
        }
        authPermissionDao.insertRolePerms(rid, pids);
        return true;
    }

}
